<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="部门：">
                        <Select class="option" v-model="search.department">
                            <Option value="全部">全部</Option>
                            <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="发布时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                        <Button v-if="role < 3" style="margin:0 10px" icon="ios-download-outline" type="success" @click="exportExcel">导出Excel</Button>
                        <Button icon="ios-color-filter-outline" type="info" @click="$_changePage('work-all','工作统计',{})">统计</Button>
                    </FormItem>

                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>
        <mySwper :isShow="isShow" :caseList="caseList" @handleMask="isShow = false"></mySwper>
    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import mySwper from '_c/swper/swper'
    import {
        exportExcel
    } from '@/api/data'
    import {
        mapGetters
    } from 'vuex'
    export default {
        name: 'active-page',
        components: {
            mySwper
        },
        data() {
            return {
                loading: false,

                isShow: false,
                // 图片列表
                caseList: [],

                // 搜索对象
                search: {
                    department: '全部',
                    start_time: '',
                    end_time: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },

                    {
                        title: '部门',
                        align: 'center',
                        key: 'department'
                    },
                    {
                        title: '发布人',
                        align: 'center',
                        key: 'username'
                    },
                    {
                        title: '名称',
                        align: 'center',
                        key: 'name'
                    },
                    {
                        title: '活动内容',
                        align: 'center',
                        key: 'content'
                    },
                    {
                        title: '活动人数',
                        align: 'center',
                        key: 'person_num'
                    },
                    {
                        title: '发布时间',
                        align: 'center',
                        key: 'c_time'
                    },
                    {
                        title: '抄送对象',
                        align: 'center',
                        key: 'copy_username'
                    },
                    {
                        title: '图片',
                        align: 'center',
                        render: (h, params) => {
                            return h('img', {
                                style: {
                                    width: '40px',
                                    height: '30px'
                                },
                                attrs: {
                                    'src': params.row.pic[0],
                                    'alt': '无'
                                },
                                on: {
                                    click: () => {
                                        this.caseList = params.row.pic
                                        this.isShow = true
                                    }
                                }
                            })
                        },
                    },

                ],
                data1: []
            }
        },
        methods: {
            ...mapGetters(['getDem', 'getRole']),

            // 获取活动列表
            getActiveList() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-work_record_active-ajax_get_wra', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getActiveList()
                        }

                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getActiveList()
            },

            // 导出
            exportExcel() {
                exportExcel('admin-work_record_active-export_wra', this.search)
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getActiveList()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getActiveList()
            },
        },
        computed: {
            demList() {
                return this.getDem()
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            this.getActiveList()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;

            .option {
                width: 150px;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>